package com.chunuo.gulimall.product.service.impl;

import com.chunuo.gulimall.product.vo.AttrRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunuo.common.utils.PageUtils;
import com.chunuo.common.utils.Query;

import com.chunuo.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.chunuo.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.chunuo.gulimall.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 删除关联关系
     * @param attrRelationVoList
     */
    @Override
    public void deleteRelation(List<AttrRelationVo> attrRelationVoList) {
        //映射封装 =》 AttrAttrgroupRelationEntity
        List<AttrAttrgroupRelationEntity> list = attrRelationVoList.stream().map((entity) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            //赋值属性
            BeanUtils.copyProperties(entity, attrAttrgroupRelationEntity);
            //返回的是AttrAttrgroupRelationEntity
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        //批量删除
        attrAttrgroupRelationDao.deleteBatchRelation(list);

    }

    /**
     * 属性分组批量添加属性的关联
     * @param attrRelationVoList
     */
    @Override
    public void addRelationAttr(List<AttrRelationVo> attrRelationVoList) {
        //将vo数据封装成entity数据
        List<AttrAttrgroupRelationEntity> collect = attrRelationVoList.stream().map((entity) -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(entity, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        //批量保存
        this.saveBatch(collect);

    }

}